package user
import java.text.SimpleDateFormat
import java.util.{Calendar, TimeZone}

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.{SparkConf, SparkContext}
import  org.apache.spark.sql.types._
import java.text.MessageFormat.format
import org.apache.spark.SparkContext._
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.sql.hive._
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
import  org.json4s.JsonAST._
import util.Util._
object userCount {
  def main(args: Array[String]) {
    // Validate args
    if (args.length == 0) {
      println("Usage: [2015-05-25|date]")
      sys.exit(1)
    }
    val date_str=args(0).toString
    //val date_str="2015-05-27"
     // val filePath="hdfs:///test/daily_user/"+"count/"
    val filePath=getConfig("user.hdfsdir")+"e_device_bind_user_total/"+date_str
    val sparkConf=new SparkConf().setAppName("user_total")
    val sc= new SparkContext(sparkConf)
    val hdfs=FileSystem.get(new Configuration())
    if(hdfs.exists(new Path(filePath)))hdfs.delete(new Path(filePath),true)
    val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc)

    import sqlContext._

  //   val user_total_sql  ="select  u_bind.product_key, u_bind.uid,g.gid   from  ( select d.product_key as product_key  ,u.uid  as uid ,d.mac as mac  from  default.mongo_binding u  left  join  default.mongo_device   d on  u.did=d.did ) u_bind   left join report.group_device_tmp g  on ( u_bind.product_key=g.product_key and u_bind.mac=g.mac)   where g.gid is not null"

    val user_total_sql=format(getConfig("user.count"),conv2ts(addDay(date_str,1).toString).toString)

    val  df=sqlContext.sql(user_total_sql)
    val pk_sql=format(getConfig("active_device.pkmap"))
    val pk=sqlContext.sql(pk_sql)
    val pkmap=pk.distinct.map(line=>line.mkString("@#"))
      .subtract( df.select("product_key")
      .distinct.map(line=>line.mkString("@#")))
      .map(line=>line.toString
      +"@#"+line.toString+"%" +"Unknown"
      )
      .flatMap(line=>line.split("@#"))
      .map(line=>(line.split("%")(0),(line,0L)))
 // finally Schema   DataFrame = [product_key: string, uid: string, gid: int]

    // 单独算 product_key 的总数.原因是:一个用户可以属于多个分组.
    val dfcount=df.map(line=>line.mkString("@#"))
      .map(line=>line.split("@#"))
      .map( line=> line(0).toString+"%"+replaceNull(line(1)).toString)
      .distinct
      .map(line=>(line.split("%")(0),1L))
      .reduceByKey(_+_)
      .map(line=>(line._1.split("%")(0),line))

    df.map(row=>row.mkString("@#"))
      .map(line=>line.split("@#"))
      .map(line=> line(0).toString+"%"+replaceNull(line(2)).toString)
       .map(line=>(line,1L))
       .reduceByKey(_ + _)
       .map(line=>(line._1.split("%")(0),line))
       .++(dfcount)
       .++(pkmap)
       .map(t=>(t._1,toUserJobejct(t._2)))
        .reduceByKey((x,y)=>x merge y)
       .map(line=>compact(render(line._2)))
       .coalesce(1, shuffle = true)
       .saveAsTextFile(filePath)

  }
}
